$(document).ready(function(){
    init();
    initFruits();
    $(".xuanzhepage").change(function() {
        findCake(1);
    });
});

function fileExport(){
    var fenleiids = $(".contenta").val();
    var name = $("input[name='name']").val();
    window.location.href = "/cake/fileExport?name=" + name + "&fenleiids=" + fenleiids;
}

function deleteBatch() {
    var data = $("input[type='checkbox'][name='supplierCheck']:checked");
    if (data.length == 0) {
        alert("请选择您要删除的数据。")
        return;
    }
    var ids = [];

    data.each(function (){
        ids.push($(this).val())
    });

    if (ids.length == 0){
        alert('未获取到要删除的数据。')
        return;
    }


    $.ajax({
        url: "/cake/deleteBatch",
        type: 'POST',
        data: {
            "ids": ids
        },
        dataType: "JSON",
        traditional:true,
        async: false,
        success: function (res) {
            if(res.code == "200") {
                alert("删除成功");
                init();
            } else {
                alert(res.msg);
            }
        },
        error: function (e) {
            alert('发生错误')
        }
    })

}

function initFruits(){
    $.ajax({
        url: "/fruits/findAll",
        type: 'GET',
        data: {},
        dataType: "JSON",
        async: false,
        success: function (res) {
            if(res.code === "200") {
                var data = res.data;
                var xlk = "";
                for (var i  = 0; i < data.length; i ++){
                    xlk += "<option class='chosena' value=" + data[i].id + ">" + data[i].name + "</option>";
                }
                $(".contenta").html(xlk);
                $(".contenta").trigger("chosen:updated");
            }else {
                alert(res.msg)
            }
        },
        error: function (e){
            alert('发生错误')
        }

    })

}

function init(){
    $.ajax({
        url: "/cake/findAll",
        type: 'GET',
        data: {},
        dataType: "JSON",
        async: false,
        success: function (res) {
            if(res.code == "200") {
                var html = "";
                var data = res.data.list;
                for (var i = 0; i < data.length; i ++){
                    html += "<tr>";
                    html += "<td><input type='checkbox' name='supplierCheck' value='" + data[i].id + "'></td>";
                    html += "<td>" + data[i].name + "</td>";
                    html += "<td>" + data[i].price + "</td>";
                    html += "<td>" + data[i].stock + "</td>";
                    html += "<td>" + data[i].createTime + "</td>";
                    html += "<td>" + data[i].guarantee + "</td>";
                    html += "<td>";
                    html += "<button class='btn btn-primary' onclick='updateById(" + data[i].id + ")'>修改 </button>";
                    html += "<button class='btn btn-primary' onclick='findById(" + data[i].id + ")'>查看 </button>";
                    html += "<button class='btn btn-primary' onclick='deleteById(" + data[i].id + ")'>删除 </button>";
                    html += "</td>";
                    html += "</tr>";
                }

                $(".tbody").html(html);

                // 尾页
                var totalPage = res.data.navigateLastPage;

                // 上一页
                var upPage = res.data.prePage;
                if (res.data.isFirstPage) {
                    upPage = 1;
                }

                // 下一页
                var lowerPage = res.data.nextPage;
                if (res.data.isLastPage) {
                    lowerPage = res.data.navigateLastPage;
                }

                $(".firstPage").attr("onClick", "findCake(1)");
                $(".upPage").attr("onClick", "findCake(" + upPage + ")");
                $(".lowerPage").attr("onClick", "findCake(" + lowerPage + ")");
                $(".totalPage").attr("onClick", "findCake(" + totalPage + ")");

                $(".tbodyInfo").html(html);

            } else {
                alert(res.msg);
            }
        },
        error: function (e) {
            alert('发生错误')
        }
    })

}

function findCake(currentPage){
    var xuanzhepage = $(".xuanzhepage").val();

    var fenleiids = $(".contenta").val();
    var name = $("input[name='name']").val();

    $.ajax({
        url: "/cake/findByCake",
        type: 'GET',
        data: {
            'fenleiids': fenleiids,
            'xuanzhepage': xuanzhepage,
            'name': name,
            'currentPage': currentPage
        },
        dataType: "JSON",
        traditional:true,
        async: false,
        success: function (res) {
            if(res.code == "200") {
                var html = "";
                var data = res.data.list;
                for (var i = 0; i < data.length; i ++){
                    html += "<tr>";
                    html += "<td><input type='checkbox' name='supplierCheck' value='" + data[i].id + "'></td>";
                    html += "<td>" + data[i].name + "</td>";
                    html += "<td>" + data[i].price + "</td>";
                    html += "<td>" + data[i].stock + "</td>";
                    html += "<td>" + data[i].createTime + "</td>";
                    html += "<td>" + data[i].guarantee + "</td>";
                    html += "<td>";
                    html += "<button class='btn btn-primary' onclick='updateById(" + data[i].id + ")'>修改 </button>";
                    html += "<button class='btn btn-primary' onclick='findById(" + data[i].id + ")'>查看 </button>";
                    html += "<button class='btn btn-primary' onclick='deleteById(" + data[i].id + ")'>删除 </button>";
                    html += "</td>";
                    html += "</tr>";
                }

                $(".tbody").html(html);

                // 尾页
                var totalPage = res.data.navigateLastPage;

                // 上一页
                var upPage = res.data.prePage;
                if (res.data.isFirstPage) {
                    upPage = 1;
                }

                // 下一页
                var lowerPage = res.data.nextPage;
                if (res.data.isLastPage) {
                    lowerPage = res.data.navigateLastPage;
                }

                $(".firstPage").attr("onClick", "findCake(1)");
                $(".upPage").attr("onClick", "findCake(" + upPage + ")");
                $(".lowerPage").attr("onClick", "findCake(" + lowerPage + ")");
                $(".totalPage").attr("onClick", "findCake(" + totalPage + ")");

                $(".tbodyInfo").html(html);

            } else {
                alert(res.msg);
            }
        },
        error: function (e) {
            alert('发生错误')
        }
    })
}

function findById(id){
    window.location.href = "/cakeView/" + id;
}


function updateById(id){
    window.location.href = "/cakeupdate/" + id;
}

function addCake(){
    window.location.href = "/addCake";
}

function deleteById(id) {

    if(!confirm("确定要删除吗")){
        return false;
    }

    $.ajax({
        url: "/cake/delete/"+ id,
        type: 'DELETE',
        data: {

        },
        dataType: "JSON",
        async: false,
        success: function (res) {
            if(res.code === "200") {
                init();
            }else {
                alert(res.msg)
            }
        },
        error: function (e){
            alert('发生错误')
        }

    })
}
